﻿using System;
using System.Collections.Generic;
using Dict.DataModel;
using Story;

namespace UI
{

    /// <summary>
    /// 英雄缘分
    /// </summary>
    public class UIHeroLuck
    {
        public TableYuan luckData;

        /// <summary>
        /// 缘分类型
        /// </summary>
        public LuckType luckType;

        private bool actived;
        //是否被激活
        public bool IsActived
        {
            get
            {
                return actived;
            }
        }

        private UIHero heroData;
        private Dictionary<string, bool> conditions;

        private int weight;
        public int Weight
        {
            get
            {
                return weight;
            }
        }


        public UIHeroLuck(TableYuan data,UIHero heroData)
        {
            this.luckData = data;
            this.heroData = heroData;

            this.weight = data.type;

            LuckHeros();
            SetLuckType();
        }

        private void LuckHeros()
        {
            conditions = new Dictionary<string, bool>();
            if (!string.IsNullOrEmpty(luckData.hero0)) conditions[luckData.hero0]=false;
            if (!string.IsNullOrEmpty(luckData.hero1)) conditions[luckData.hero1]=false;
            if (!string.IsNullOrEmpty(luckData.hero2)) conditions[luckData.hero2]=false;
            if (!string.IsNullOrEmpty(luckData.hero3)) conditions[luckData.hero3]=false;
            if (!string.IsNullOrEmpty(luckData.treaId0)) conditions[luckData.treaId0]=false;
            if (!string.IsNullOrEmpty(luckData.equip0)) conditions[luckData.equip0]=false;

#if UNITY_EDITOR
            if(conditions.Count<=0)
            {
                UnityEngine.Debug.LogError("[ERROR] 缘分数据有问题：ID=" + luckData.id);
            }
#endif
        }
        
        private void SetLuckType()
        {
            /*
            if (!string.IsNullOrEmpty(luckData.hero0) || 
                !string.IsNullOrEmpty(luckData.hero1) || 
                !string.IsNullOrEmpty(luckData.hero3) ||
                !string.IsNullOrEmpty(luckData.hero2))
            {
                this.luckType = LuckType.Hero;
            }
            else if (!string.IsNullOrEmpty(luckData.treaId0))
            {
                this.luckType = LuckType.Treasure;
            }
            else if (!string.IsNullOrEmpty(luckData.equip0))
            {
                this.luckType = LuckType.Equipment;
            }*/

            if( luckData.type == 1 || 
                luckData.type == 2 || 
                luckData.type == 3 || 
                luckData.type == 4 )
            {
                this.luckType = LuckType.Hero;
            }
            else if(luckData.type == 5)
            {
                this.luckType = LuckType.Equipment;
            }
            else if(luckData.type == 6)
            {
                this.luckType = LuckType.Treasure;
            }
#if UNITY_EDITOR
            else
            {
                UnityEngine.Debug.LogError("[ERROR] 缘分数据表，类型数据有问题 ID=" + luckData.id);
            }
#endif
        }

        /// <summary>
        /// 刷新激活状态
        /// </summary>
        public void FreshLuckActived()
        {
            ResetActive();

            if (luckType == LuckType.Hero)
            {
                //上阵
                var battleCamps = StaticData.playerData.InstPlayerData.BattleCamps;
                foreach (var it in battleCamps)
                {
                    if (it.Value != null && it.Value.hero != null && it.Value.hero.heroData != null)
                    {
                        var heroId = it.Value.hero.heroId;
                        if (conditions.ContainsKey(heroId))
                        {
                            conditions[heroId] = true;
                        }
                    }
                }

                //助阵
                var helpCamps = StaticData.playerData.InstPlayerData.GetHelpCamps();
                foreach (var it in helpCamps)
                {
                    var hero = StaticData.playerData.GetUIHero(it.Value.guid);
                    if (hero != null&&conditions.ContainsKey(hero.heroId))
                    {
                        conditions[hero.heroId] = true;
                    }
                }
            }
            else
            {
                var battleCamps = StaticData.playerData.InstPlayerData.BattleCamps;
                foreach (var hero in battleCamps)
                {
                    if (hero.Value != null && hero.Value.hero!=null )
                    {
                        //说明他在出战里面
                        if (heroData.heroId == hero.Value.hero.heroId)
                        {
                            //说明有这个装备
                            foreach (var equip in hero.Value.equipments)
                            {
                                if (equip != null && conditions.ContainsKey(equip.equipId))
                                {
                                    conditions[equip.equipId] = true;
                                    break;
                                }
                            }

                            //说明有这个戒子
                            foreach (var trea in hero.Value.treasures)
                            {
                                if (trea != null && conditions.ContainsKey(trea.treasureId))
                                {
                                    conditions[trea.treasureId] = true;
                                }
                            }
                        }
                    }
                }
            }

            actived = true;
            foreach(var kvp in conditions)
            {
                if(kvp.Value==false)
                {
                    actived = false;
                    break;
                }
            }
            

        }

        /// <summary>
        /// 重置缘分
        /// </summary>
        public void ResetActive()
        {
            actived = false;
            LuckHeros();
        }
    }
}
